package org.myorg;

import java.io.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.io.ObjectWritable;


public class RWF extends Para2 {
	
	private static final Log LOG = LogFactory.getLog(RWF.class);
	//static public String hp = "/home/stanley/Documents/workspace/hadoop/hadoop-0.20.2";
	//static public String hp = "/home/stanley/hadoop-0.20.2";
	
	static public double rf() throws NumberFormatException, IOException {
		double bv = Double.MAX_VALUE;
		BufferedReader in = null;
		try {
			in = new BufferedReader(new FileReader(hp + "/res/part-00000"));
		}
		catch (IOException ex) {
			ex.printStackTrace();
		}
		String s = new String();

		while((s= in.readLine()) !=null)
		{

			//System.out.println(s);
			String[]   tokens   =   s.split("\t"); 
			if (Double.parseDouble(tokens[1]) < bv) {
				bv = Double.parseDouble(tokens[1]);
			}

			double[]   data   =   new   double[tokens.length]; 
			for   (int   i   =   0;   i   <   data.length;   i++)   { 
				data[i]   =   Double.parseDouble(tokens[i]); 
				//System.out.println(data[i]);
			}
		}
		return bv;
	}

	static public void wf(String content) {
		File writefile;
		String path = hp + "/res/res";
		content = content + "\n";
		try {
			// 通过这个对象来判断是否向文本文件中追加内容
			// boolean addStr = append;
			writefile = new File(path);
			// 如果文本文件不存在则创建它
			if (writefile.exists() == false) {
				writefile.createNewFile();
				writefile = new File(path); // 重新实例化
			}
			FileOutputStream fw = new FileOutputStream(writefile,true);
			//System.out.println("###content:" + content);
			fw.write(content.getBytes());
			fw.flush();
			fw.close();
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
	}

	static public double rf(String path) throws NumberFormatException, IOException {
		double bv = Double.MAX_VALUE;
		BufferedReader in = null;
		try {
			in = new BufferedReader(new FileReader(path));
		}
		catch (IOException ex) {
			ex.printStackTrace();
		}
		String s = new String();

		while((s= in.readLine()) !=null)
		{

			//System.out.println(s);
			String[]   tokens   =   s.split("\t"); 
			if (Double.parseDouble(tokens[1]) < bv) {
				bv = Double.parseDouble(tokens[1]);
			}

			double[]   data   =   new   double[tokens.length]; 
			for   (int   i   =   0;   i   <   data.length;   i++)   { 
				data[i]   =   Double.parseDouble(tokens[i]); 
				//System.out.println(data[i]);
			}
		}
		return bv;
	}
	
	static public StringBuffer rfs(String path){
		
		StringBuffer sb = new StringBuffer();
		BufferedReader in = null;
		try {
			in = new BufferedReader(new FileReader(path));
		}
		catch (IOException ex) {
			ex.printStackTrace();
		}
		String s = new String();

		try {
			while((s= in.readLine()) !=null)
			{
				sb.append(s + "\n");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sb;
	}

	static public void wf(String content, String path) {
		File writefile;

		if (!content.endsWith("\n"))
			content = content + "\n";
		try {
			// 通过这个对象来判断是否向文本文件中追加内容
			// boolean addStr = append;
			writefile = new File(path);
			// 如果文本文件不存在则创建它
			if (writefile.exists() == false) {
				writefile.createNewFile();
				writefile = new File(path); // 重新实例化
			}
			FileOutputStream fw = new FileOutputStream(writefile,true);
			//System.out.println("###content:" + content);
			fw.write(content.getBytes());
			fw.flush();
			fw.close();
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}

	}

	public static void wp(FPeople p, String path) {
		ObjectOutputStream output = null;
		try {
			output = new ObjectOutputStream(new FileOutputStream(path));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			output.writeObject(p);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void nwp(FPeopleWritable p, String path, boolean append) {
		DataOutputStream output = null;
		try {
			output = new DataOutputStream(new FileOutputStream(path,append));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			p.write(output);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static FPeople rp(String path) {
		FPeople p = null;
		ObjectInputStream input = null;
		try {
			input = new ObjectInputStream(new FileInputStream(path));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			p = (FPeople)(input.readObject());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return p;
	}
	
/*	public static void wo(Object p, String path) {
		ObjectWritable output = null;
		
		
		try {
			output = new ObjectWritable(new FileOutputStream(path));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			System.out.println("lll");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}*/

	public static void delf(String path) {
		Process p = null;
		String cmds = "";
		try {
			cmds = hp + "/bin/hadoop fs -rmr " + path;
			p = Runtime.getRuntime().exec(cmds);
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			if (p.waitFor() == 0) {
				LOG.info("Deleting file execute result is OK!");
			} else {
				LOG.info(cmds + "Deleting execute result is fail......");
			}
		} catch (InterruptedException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
	}
	
	public static void inf(String local, String dest) {
		Process p = null;
		try {
			String cmds = hp + "/bin/hadoop fs " +
			"-put " + local + " " + dest;
			p = Runtime.getRuntime().exec(cmds);
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			if (p.waitFor() == 0) {
				LOG.info("Puting file execute result is OK!");
			} else {
				LOG.info("Puting file execute result is fail......");
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void outf(String remote, String dest) {
		Process p = null;
		try {
			String cmds = hp + "/bin/hadoop fs " +
			"-get " + remote + " " + dest;
			p = Runtime.getRuntime().exec(cmds);
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			if (p.waitFor() == 0) {
				LOG.info("Geting file execute result is OK!");
			} else {
				LOG.info("Geting file execute result is fail......");
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void mvf(String src, String dst) {
		Process p = null;
		try {
			String cmds = hp + "/bin/hadoop fs " +
			"-mv " + src + " " + dst;
			p = Runtime.getRuntime().exec(cmds);
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			if (p.waitFor() == 0) {
				LOG.info("Geting file execute result is OK!");
			} else {
				LOG.info("Geting file execute result is fail......");
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void execmd(String cmd) {
		Process p = null;
		try {
			p = Runtime.getRuntime().exec(cmd);
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			if (p.waitFor() == 0) {
				LOG.info("Command execute result is OK!");
			} else {
				LOG.info("Command " + cmd + " execute result is fail......");
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
}
